import type { ErrorResponse } from '@/utils/request'
import errorList, { ActionMethodType, type ErrorActionInfo, type wxErrorInfo } from '@/constants/Errors'
import loginHelper from '@/pluginHelper/login'
import { actionDebounce, showModal, toPage } from '@/utils/mpHelper'
import { useAuthStore } from '@/store/auth'
import { useApplianceStore } from '@/store/appliance'
import { debounce } from 'lodash'

/**
 * 无网络，或者请求超时处理方法
 *
 * @param error
 */
export function networkErrorHandle(error: wxErrorInfo) {
  console.log('networkErrorHandle', error)
  if (error.errMsg && (error.errMsg.indexOf('timeout') !== -1 || error.errMsg.indexOf('request:fail') !== -1)) {
    wx.hideLoading({
      noConflict: true,
      complete:()=>{
        uni.showToast({
          duration: 3000,
          title: `【E${error.errno ? error.errno : error.errCode ? error.errCode : ''}】网络异常`,
          icon: 'none',
        })
      }
    })
  }
}
const authStore = useAuthStore()
let isModalShowing = false

/**
 *接口请求错误统一处理
 * @param errorResponse
 */
export function httpRequestErrorHandle(errorResponse: ErrorResponse) {
  let errorActionInfo: ErrorActionInfo = {
    code: 0,
    message: '',
    promptText: '',
    presentOrNot: false,
  }
  uni.hideLoading()
  for (const res of errorList) {
    if (res.code == errorResponse.code) {
      if (errorResponse.code === 409 || errorResponse.code === 404) {
        if (errorResponse.data.key == res.key) {
          errorActionInfo = res
          break
        }
      } else {
        errorActionInfo = res
        break
      }
    }
  }
  if (errorActionInfo.code === 0) {
    if (isModalShowing) return
    isModalShowing = true
    showModal({
      content: `【E${errorResponse.code}】${errorResponse.msg}`,
      showCancel: false,
      confirmText: errorActionInfo.actionName,
      confirmColor: '#007aff',
    }).then((res) => {
      isModalShowing = false
    })
  }
  if (!errorActionInfo.presentOrNot) {
    return
  }
  if (errorActionInfo.actionType === 0) {
    wx.hideLoading({
      noConflict: true,
      complete:()=>{
        uni.showToast({
          duration: 3000,
          title: `【E${errorActionInfo.code}】${errorActionInfo.promptText}`,
          icon: 'none',
        })
      }
    })
  } else if (errorActionInfo.actionType == 1) {
    if (isModalShowing) return
    isModalShowing = true
    let content = `【E${errorActionInfo.code}】${errorActionInfo.promptText}`
    if (errorResponse.code === 409 || errorResponse.code === 10015 || errorResponse.code === 20012 || errorResponse.code === 20015 ||
      errorResponse.code === 40001 || errorResponse.code === 40002 || errorResponse.code === 40003 ||
      errorResponse.code === 40004 || errorResponse.code === 40006 || errorResponse.code === 40007 || errorResponse.code === 40014 ||
      errorResponse.code === 60004 || errorResponse.code === 60016 || errorResponse.code === 60017) {
      content = `${errorActionInfo.promptText}`
    }
    showModal({
      content: content,
      showCancel: false,
      confirmText: errorActionInfo.actionName,
      confirmColor: '#007aff',
    }).then(() => {
      isModalShowing = false
    })
  } else if (errorActionInfo.actionType == 2) {
    if (authStore.needRefreshToken) {
      return
    }
    if (isModalShowing) return
    isModalShowing = true
    showModal({
      content: `【E${errorActionInfo.code}】${errorActionInfo.promptText}`,
      showCancel: false,
      confirmText: errorActionInfo.actionName,
      confirmColor: '#007aff',
    }).then(() => {
      if (errorActionInfo.actionMethod === ActionMethodType.LOGIN) {
        isModalShowing = false

        authStore.removePartialUserInfo()

        loginHelper.noCiamIdLogin()
      } else if (errorActionInfo.actionMethod === ActionMethodType.GOTOPAIR) {
        isModalShowing = false
        const applianceStore = useApplianceStore()
        const path = '/pages/pairing/confirmAppliance/index'
        toPage(path, { vib: applianceStore.current?.vib, pairingSource: '用户没有智能音箱' })
      }
    })
  }
}
